package main

import "fmt"

func main() {
	testHappensBefore()
}

// testHappensBefore
//
//	@Description:
//	要保证 read绝对能观察到write操作的结果，满足两个条件:
//
// 1. write happens before read
// 2. 其他的write(n) 要么happens before write ,要么happens after read
func testHappensBefore() {
	//
	var a int = 1
	//write-n...
	write(&a)
	read(&a)
	//write-n ...
}

func read(a *int) {
	fmt.Println("read()读取到结果：", *a)
}

func write(a *int) {
	fmt.Println("write()写入结果：", 101)
	*a = 101
}

func writeN(a *int) {
	fmt.Println("writeN()写入结果：", 999)
	*a = 999
}

// init
//
//	@Description: main 函数一定在导入的包的 happensBeforeInit 函数之后执行
func init() {

}
